package com.student.system2;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    private static final String ADD_STUDENT="1";
    private static final String DELETE_STUDENT="2";
    private static final String UPDATE_STUDENT="3";
    private static final String QUERY_STUDENT="4";
    private static final String EXIT="5";

    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("======欢迎来到黑马学生管理系统======");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("================================");
            System.out.print("请输入你的需要操作的序号:");
            Scanner scanner = new Scanner(System.in);
            String choose = scanner.next();
            switch (choose) {
                case ADD_STUDENT -> addStudent(list);
                case DELETE_STUDENT -> deleteStudent(list);
                case UPDATE_STUDENT -> updateStudent(list);
                case QUERY_STUDENT -> queryStudent(list);
                case EXIT -> {
                    System.out.println("退出系统");
                    System.exit(0);
                }
                default -> System.out.println("输入错误");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        //利用空参构造创建学生对象
        Student student = new Student();
        while (true) {
            System.out.print("请输入学生的ID:");
            String id = scanner.next();
            boolean flag = contains(list, id);
            if(flag){
                //ID已经存在，请重新录入
                System.out.println("ID已经存在，请重新录入!");
            }else {
                //ID不存在，可以使用
                student.setId(id);
                break;
            }
        }

        System.out.print("请输入学生的姓名:");
        String name = scanner.next();
        student.setName(name);

        System.out.print("请输入学生的年龄:");
        int age = scanner.nextInt();
        student.setAge(age);

        System.out.print("请输入学生的住址:");
        String address = scanner.next();
        student.setAddress(address);
        //把学生对象添加到集合中
        list.add(student);
        //添加提示
        System.out.println("学生信息添加成功!");
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的ID:");
        String id = scanner.next();
        //查询id在集合中的索引
        int index = getIndex(list, id);
        //对index进行判断
        if(index==-1){
            //如果-1，就表示不存在，结束方法，回到初始菜单
            System.out.println("id不存在，删除失败!");
        }else {
            //如果大于等于0的，表示存在，直接删除
            list.remove(index);
            System.out.println("id为"+id+"删除成功!");
        }
    }
    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改学生的ID:");
        String id = scanner.next();
        int index = getIndex(list, id);
        if(index==-1){
            System.out.println("要修改的ID:"+id+"不存在!");
        }else {
            //当代码执行到这里，表示什么?表示当前id是存在的。
            //获取要修改的学生对象
            Student student = list.get(index);
            //输入其他信息并修改
            System.out.print("请输入要修改的学生姓名:");
            String name = scanner.next();
            student.setName(name);
            System.out.print("请输入要修改的学生年龄:");
            int age = scanner.nextInt();
            student.setAge(age);
            System.out.print("请输入要修改的学生住址:");
            String address = scanner.next();
            student.setAddress(address);
            System.out.println("学生信息修改成功!");
        }
    }
    //查询学生
    public static void queryStudent(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再进行查询!");
            //结束方法
            return;
        }
        //打印表头信息
        System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress());
        }
    }

    //判断ID在集合中是否存在
    public static boolean contains(ArrayList<Student> list, String id){
        //循环遍历集合得到里面的每一个学生对象
        for (int i = 0; i < list.size(); i++) {
            //拿到学生对象后,获取id并进行判断
            Student student = list.get(i);
            if(student.getId().equals(id)){
                //存在，true
                return true;
            }
        }
        //不存在，false
        return false;
    }
    //根据ID在集合中获取索引
    public static int getIndex(ArrayList<Student> list, String id){
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //获取每一个学生对象
            Student student = list.get(i);
            //拿着集合中的学生id跟要查询的id进行比较
            if(student.getId().equals(id)){
                //如果一样，那么返回索引
                return i;
            }
        }
        //当循环结束之后还没找到，表示不存在，返回-1
        return -1;
    }
}
